package com.teamtgv.shareshark.task;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import com.teamtgv.shareshark.AppData;
import com.teamtgv.shareshark.JSONParser;
import com.teamtgv.shareshark.ShareActivity;
import com.teamtgv.shareshark.UserData;

/**
 * Background Async Task to Create new product
 * */
public class InsertRecommendationTask extends AsyncTask<String, String, Integer> {

	public static final String LOG_TAG = InsertRecommendationTask.class.getSimpleName();
	
	private ShareActivity activity;
	private ProgressDialog pDialog;
	private String what;
	private String where;
	private BigInteger targetFacebookId;
	
	public InsertRecommendationTask(ShareActivity activity, String what, String where, BigInteger targetFacebookId) {
		this.activity = activity;
		this.what = what;
		this.where = where;
		this.targetFacebookId = targetFacebookId;
	}
	
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Creating recommendation..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected Integer doInBackground(String... args) {
        
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("what", what));
        params.add(new BasicNameValuePair("where", where));
        params.add(new BasicNameValuePair("to", targetFacebookId.toString()));
        params.add(new BasicNameValuePair("by", UserData.getUserId().toString()));
        
        Log.v(LOG_TAG, "Adding to database --- BY: '" + UserData.getUsername() + "' WHAT: '" + what + "', WHERE: '" + where + "', TO: '" + targetFacebookId.toString() + "'");
        
        JSONParser jsonParser = new JSONParser();
        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(AppData.FULL_HOST + "insert_recommendation.php", "POST", params);
        
        if (json == null) {
        	Log.v(LOG_TAG, "JSON object from HTTP request is null");
        	return null;
        }
        
        // check log cat fro response
        Log.v("JSON Response: ", json.toString());

        // check for success tag
        try {
            int success = json.getInt("success");
            Log.v(LOG_TAG, "JSON Adding response: " + json.getString("message"));
            if (success == 1) {
                // closing this screen
                return json.getInt("id");
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(Integer result) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }
}